package com.itheima.crops.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.crops.dao.DiseaseMapper;
import com.itheima.crops.pojo.Disease;
import com.itheima.crops.pojo.Index;
import com.itheima.crops.service.DiseaseService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class DiseaseServiceImpl implements DiseaseService {
    @Autowired
    private DiseaseMapper diseaseMapper;

    /**
     * 农作物病虫害全查
     * @return
     */
    @Override
    public List<Disease> findAll() {
        List<Disease> diseaseList = diseaseMapper.selectAll();
        return diseaseList;
    }

    /**
     * 农作物病虫害根据id查询
     * @param id
     * @return
     */
    @Override
    public Disease findById(Integer id) {
        Disease disease = diseaseMapper.selectByPrimaryKey(id);
        if(disease == null) {
            throw new RuntimeException("农作物病虫害不存在");
        }
        return disease;
    }

    /**
     * 新增农作物病虫害
     * @param disease
     */
    @Override
    public void add(Disease disease) {
        diseaseMapper.insertSelective(disease);
    }

    /**
     * 修改农作物病虫害
     * @param disease
     */
    @Override
    public void update(Disease disease) {
        Disease disease1 = diseaseMapper.selectByPrimaryKey(disease.getId());
        if(disease1 == null) {
            throw new RuntimeException("农作物病虫害不存在");
        }
        diseaseMapper.updateByPrimaryKey(disease);
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Disease> findPage(Map<String,Object> searchMap, int page, int size){
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Disease>)diseaseMapper.selectByExample(example);
    }

    /**
     * 删除农作物病虫害
     * @param id
     */
    @Override
    @Transactional
    public void delete(Integer id) {
        Disease disease = diseaseMapper.selectByPrimaryKey(id);
        if(disease == null) {
            throw new RuntimeException("农作物病虫害不存在");
        }
        List<Integer> cids = diseaseMapper.findCidsByDid(id);
        if(cids != null && cids.size() > 0) {
            throw new RuntimeException("该病虫害与农作物关联");
        }
        diseaseMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Disease.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 主键
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            // 病虫害名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name", "%"+searchMap.get("name")+"%");
            }
        }
        return example;
    }
}
